package com.black.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.common.core.domain.entity.SysRole;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.system.domain.req.system.query.SysRoleQueryReq;
import com.black.system.domain.resp.system.SysRoleResp;
import com.black.system.mapper.SysRoleMapper;
import com.black.system.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【sys_role(角色信息表)】的数据库操作Service实现
 * @createDate 2024-08-19 10:07:23
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole>
        implements SysRoleService {

    /**
     * 根据用户ID查询角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysRole> findRolesByUserId(Long userId) {
        return baseMapper.findRolesByUserId(userId);
    }

    /**
     * 分页查询角色信息
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<SysRoleResp> paginationQuery(SysRoleQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            baseMapper.paginationQuery(req);
        });
    }

    /**
     * 根据id查询角色信息
     *
     * @param id
     * @return
     */
    @Override
    public SysRoleResp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }
        // 查询
        SysRole sysRole = this.getById(id);
        if (Objects.isNull(sysRole)) {
            throw new RecordNotFoundException();
        }
        SysRoleResp resp = new SysRoleResp();
        BeanUtils.copyProperties(sysRole, resp);
        return resp;
    }

    /**
     * 导出角色列表
     *
     * @param req
     * @param ids
     * @return
     */
    @Override
    public List<SysRoleResp> export(SysRoleQueryReq req, Long[] ids) {
        List<SysRoleResp> dataList;
        // 根据主键导出
        if (Objects.nonNull(ids) && ids.length > 0) {
            dataList = baseMapper.export(ids);
        } else {
            // 根据条件导出
            dataList = baseMapper.paginationQuery(req);
        }
        return dataList;
    }
}




